require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroLinkHandler = {};
local this = RedDotsHeroLinkHandler;

local rankConfig = nil;

function RedDotsHeroLinkHandler.init()
    CloseEvent(Config.EventType.Fresh_HeroLink, this.update);
    CloseEvent(Config.EventType.Fresh_Level, this.update);
    CloseEvent(Config.EventType.Refresh_HeroLinkUpgrade, this.update);

    ListenEvent(Config.EventType.Fresh_HeroLink, this.update);
    ListenEvent(Config.EventType.Fresh_Level, this.update);
    ListenEvent(Config.EventType.Refresh_HeroLinkUpgrade, this.update);

    if rankConfig == nil then
        rankConfig = G.dataTable["t_herorank"];
    end

    return this;
end

--参数1：指定英雄type，nil代表检查所有英雄类型
---英雄智慧之链红点
function RedDotsHeroLinkHandler.getFlag(...)
    local flag = Config.RedDotsFlagState.None;
    --if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroLink, false) then
    --    --local typeId = ...;
    --    --local heroTypeList = {};
    --    --if typeId ~= nil then
    --    --    table.insert(heroTypeList, typeId);
    --    --else
    --    --    heroTypeList = {1, 2, 3, 4, 5, 6};
    --    --end
    --    local heroLinkCfg = logicMgr.HeroLinkManager.getAllLinkTableData();
    --    local index = 1;
    --    for k, v in pairs(heroLinkCfg) do
    --        for i = 1, 6 do
    --            flag = this.getFlagById(index);
    --            if flag == Config.RedDotsFlagState.Needful then
    --                break ;
    --            end
    --            index = index + 1;
    --        end
    --    end
    --    if flag == Config.RedDotsFlagState.Needful then
    --        flag = this.cheakHero();
    --    end
    --    if flag == Config.RedDotsFlagState.None then
    --        flag = logicMgr.HeroLinkManager.TopLevelRedDot() and Config.RedDotsFlagState.Needful or flag
    --    end
    --end
    return flag;
end
function RedDotsHeroLinkHandler.cheakHero()
    local heroList = logicMgr.HeroManager.getOwnerHeroList(nil,nil,true);
    for k, v in pairs(heroList) do
        if v.level == 1 and not logicMgr.HeroLinkManager.isInLink(v.partnerId, true) then
            return Config.RedDotsFlagState.Needful;
        end
    end
    return Config.RedDotsFlagState.None;
end
function RedDotsHeroLinkHandler.getFlagById(index)
    local flag = Config.RedDotsFlagState.None;
    local heroLinkInfo = logicMgr.HeroLinkManager.getLinkInfoBySlotId(index);
    if heroLinkInfo ~= nil then
        local ok = true;
        if heroLinkInfo.lockType == 1 then
            if heroLinkInfo.unLock > logicMgr.MapManager.getMapId() then
                ok = false;
            end
        end
        if ok and HelperFunc.GetServerTime() - heroLinkInfo.lastRemoveTime >= G.dataTable["t_global"]["58"].f_int_value then
            if heroLinkInfo.partnerId == nil or #heroLinkInfo.partnerId == 0 then
                flag = Config.RedDotsFlagState.Needful;
            end
        end
    end
    return flag;
end

function RedDotsHeroLinkHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroLink);
end